import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/** This checkstyle custom-check requires > 1 character variable names.
 *   @author jdalbey
 *   @version 0.2    2013.11.2   for checkstyle 5.6
 */  
public class VariableNameLengthCheck
    extends Check
{
    private int kMinLength = 2;

    /** Returns the default token a check is interested in. 
     *  Only used if the configuration for a check does not define the tokens.
     *  @return int[] of default tokens
     */
    public int[] getDefaultTokens()
    {
        return new int[] { TokenTypes.VARIABLE_DEF };
    }

    /** Visit a token in the AST.  Here's where we do the real work
     *  of the custom check.  
     *  @param paramDetailAST is the AST detail
     */
    public void visitToken(DetailAST paramDetailAST)
    {
        DetailAST localDetailAST = paramDetailAST.findFirstToken(TokenTypes.IDENT);

        String str = localDetailAST.getText();
        /*  Is the length of the identifier less than allowed? */
        if (str.length() < kMinLength) 
        {
            log(paramDetailAST.getLineNo(), 
                "One character variable name not allowed: " + str);
        }
    }
}
